<?php
namespace BtcTool;

use Nbobtc\Http\Client as C;
use Nbobtc\Command\Command;

class Client extends C {
    public function __construct($dsn){
        parent::__construct($dsn);
    }
    
    //params: cmd p1 p2 ...
    public function exec(){
    $params = func_get_args();      
        $method = array_shift($params);
        
        $cmd = new Command($method,$params);
        $rsp = $this->sendCommand($cmd);
        
        //todo: check status code
        $rsp = $rsp->getBody()->getContents();              
        $rsp = json_decode($rsp);
        
        //for debug
        if(isset($rsp->error)) {
            echo '=============ERROR==============================' . PHP_EOL;      
            //echo 'api : ' . $method . ' ' . implode(',',$params) . PHP_EOL;
            echo 'api : ' . $method . PHP_EOL;
            echo 'code : ' . $rsp->error->code . ' message : ' . $rsp->error->message . PHP_EOL;
            echo '=================================================' . PHP_EOL;
        }
        
        return $rsp;
    }
}